#include "TimeBar.h"
#include "GraphicFactory.h"
#include "Macro.h"
#include "BltInfo.h"

namespace Implement
{
	using Graphics::Color;

	TimeBar::TimeBar(Graphics::Drawable* image, DWORD maxValue, 
					 DWORD maxWidth,
					 DWORD maxHeight,
					 bool  isHorizontal):
			m_Pos(0,0),
			m_MaxValue(maxValue),
			m_MaxWidth(maxWidth),
			m_MaxHeight(maxHeight),
			m_Now(1L),
			m_IsHorizontal(isHorizontal)
	{
		if(m_MaxValue<1L)
		{
			m_MaxValue = 1L;
		}
		if(image==0)
		{
			throw std::exception("no image");
		}
		this->SetImage(image);
		if(m_IsHorizontal)
		{
			this->SetZoomY(m_MaxHeight/(float)this->GetImage()->GetHeight());
		}
		else
		{
			this->SetZoomX(m_MaxWidth/(float)this->GetImage()->GetWidth());
		}
		m_ColorLT = Color(0,80,60);
		m_ColorRB = Color(200,240,20);
		m_Step = 60;
		this->GetInfo().GetDiffuse()[0] = Color(50,20,10);
		this->GetInfo().GetDiffuse()[1] = m_ColorLT;
		this->GetInfo().GetDiffuse()[2] = Color(120,20,160);
		this->GetInfo().GetDiffuse()[3] = m_ColorRB;
	}
	float TimeBar::GetRate(void)
	{
		return m_Now / (float) m_MaxValue;
	}
	const Position& TimeBar::GetPos(void)
	{
		return m_Pos;
	}
	void TimeBar::SetPos(int x,int y)	
	{
		m_Pos.Set(x,y);
	}
	int TimeBar::GetSequence(void)
	{
		return 1;
	}
	bool TimeBar::RenderImpl()
	{
		Color& lt = this->GetInfo().GetDiffuse()[1];
		Color& rb = this->GetInfo().GetDiffuse()[3];

		lt.Set(lt.GetRed() + (m_ColorRB.GetRed() - lt.GetRed())/m_Step,
			   lt.GetGreen() + (m_ColorRB.GetGreen() - lt.GetGreen())/m_Step,
			   lt.GetBlue() + (m_ColorRB.GetBlue() - lt.GetBlue())/m_Step,
			   lt.GetAlpha());
		rb.Set(rb.GetRed() + (m_ColorLT.GetRed() - rb.GetRed())/m_Step,
			   rb.GetGreen() + (m_ColorLT.GetGreen() - rb.GetGreen())/m_Step,
			   rb.GetBlue() + (m_ColorLT.GetBlue() - rb.GetBlue())/m_Step,
			   rb.GetAlpha());
		m_Step --;

		if(m_Step == 1)
		{
			m_Step = 60;
			Color temp = m_ColorLT;
			m_ColorLT = m_ColorRB;
			m_ColorRB = temp;
		}

		if(m_Now < m_MaxValue)
		{
			m_Now ++;
		}
		if(m_IsHorizontal)
		{
			this->SetZoomX(m_Now * m_MaxWidth /(float)(m_MaxValue * this->GetImage()->GetWidth()));
		}
		else
		{
			this->SetZoomY(m_Now * m_MaxHeight /(float)(m_MaxValue * this->GetImage()->GetHeight()));
		}
		return SpriteSingle::RenderImpl();
	}
}
